package com.egu.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.egu.mapper.DepartmentInfoMapper;
import com.egu.mapper.UserMapper;
import com.egu.model.DepartmentInfo;
import com.egu.model.DepartmentInfoExample;
import com.egu.model.DepartmentInfoExample.Criteria;
import com.egu.model.User;
import com.egu.model.UserExample;
import com.egu.service.DepartmentInfoService;
import com.egu.vo.PageParam;
import com.egu.vo.RequestPageVo;
import com.egu.vo.RequestResultVo;

@Service
public class DepartmentInfoServiceImpl implements DepartmentInfoService {

	@Autowired
	DepartmentInfoMapper departmentInfoMapper;

	@Autowired 
	UserMapper userMapper;
	
	@Override
	public RequestResultVo<List<DepartmentInfo>> getAllList() {
		DepartmentInfoExample example = new DepartmentInfoExample();
		example.createCriteria().andIsUsedEqualTo("1");
		List<DepartmentInfo> list = departmentInfoMapper.selectByExample(example);
		return RequestResultVo.success(list);
	}

	@Override
	public RequestPageVo<DepartmentInfo> getList(PageParam param, DepartmentInfo departmentInfo) {
		RequestPageVo<DepartmentInfo> result = new RequestPageVo<DepartmentInfo>();
		DepartmentInfoExample example = new DepartmentInfoExample();
		Criteria criteria = example.createCriteria();
		criteria.andIsUsedEqualTo("1");
		if (StringUtils.isNotBlank(departmentInfo.getDepartmentName())) {
			criteria.andDepartmentNameLike("%" + departmentInfo.getDepartmentName() + "%");
		}
		int count = (int) departmentInfoMapper.countByExample(example);
		if (count > 0) {
			example.setStartPage(param.getOffset());
			example.setPageSize(param.getLimit());
			List<DepartmentInfo> list = departmentInfoMapper.selectByExample(example);
			result.setRows(list);
			result.setTotal(count);
		}
		return result;
	}

	@Override
	@Transactional
	public RequestResultVo<DepartmentInfo> deleteDataById(Integer id) {
		DepartmentInfo departmentInfo = new DepartmentInfo();
		departmentInfo.setId(id);
		departmentInfo.setIsUsed("0");
		try {
			departmentInfoMapper.updateByPrimaryKeySelective(departmentInfo);
			
			UserExample userExample = new UserExample();
			com.egu.model.UserExample.Criteria createCriteria = userExample.createCriteria();
			createCriteria.andDepartmentIdEqualTo(id);
			createCriteria.andIsUsedEqualTo("1");
			List<User> list = userMapper.selectByExample(userExample);
			
			if(list.size()>0) {
				for (User user : list) {
					user.setIsUsed("0");
					userMapper.updateByPrimaryKeySelective(user);
				}
			}
			
			
		} catch (Exception e) {
			return RequestResultVo.error("数据库删除失败");
		}
		return RequestResultVo.success();
	}

	@Override
	@Transactional
	public RequestResultVo<DepartmentInfo> saveOrUpdate(DepartmentInfo departmentInfo) {
		RequestResultVo<DepartmentInfo> resultVo = new RequestResultVo<>();
		if (departmentInfo.getId() == null) {
			DepartmentInfo dp = this.checkDepartMentExistByDpName(departmentInfo.getDepartmentName());
			if (dp != null) {
				resultVo.setSuccess(false);
				resultVo.setErrorMessage("该部门已经存在");
				return resultVo;
			}
			departmentInfoMapper.insertSelective(departmentInfo);
		} else {
			departmentInfoMapper.updateByPrimaryKeySelective(departmentInfo);
		}
		return resultVo;
	}

	private DepartmentInfo checkDepartMentExistByDpName(String departmentName) {

		DepartmentInfoExample example = new DepartmentInfoExample();
		Criteria criteria = example.createCriteria();
		criteria.andDepartmentNameLike("%" + departmentName + "%");
		criteria.andIsUsedEqualTo("1");
		List<DepartmentInfo> list = departmentInfoMapper.selectByExample(example);
		if (list.size() > 0) {
			DepartmentInfo departmentInfo = list.get(0);
			return departmentInfo;
		}
		return null;
	}

}
